﻿using CCWin;
using CCWin.SkinControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebSocketChat.Common;
using WebSocketChat.Common.Client;

namespace WebSocketChat.Client
{
    public partial class ChatForm : CCSkinMain
    {
        public ClientSocket Current { get; set; }
        public ChatListSubItem FormItem { get; private set; }
        public Font ChatFont;
        private Dictionary<uint, Image> DefaultEmotions = new Dictionary<uint, Image>();
        public ChatForm()
        {
            InitializeComponent();
        }
        public ChatForm(ChatListSubItem item, ClientSocket user)
        {
            InitializeComponent();
            Current = user;
            FormItem = item;
        }

        public void AppendMessage(string text)
        {
            tbShow.AppendRichText(text + " " + DateTime.Now.ToString() + Environment.NewLine, ChatFont, Color.Blue);
            tbShow.ScrollToCaret();
        }

        public void MessageArrive(IMessage msg)
        {
            tbShow.AppendRichText(FormItem.NicName + " " + DateTime.Now.ToString() + Environment.NewLine, ChatFont, Color.Blue);
            var content = msg.Data as ChatBoxContent;
            if (content != null)
                tbShow.AppendChatBoxContent(content);
            tbShow.AppendText(Environment.NewLine);
            tbShow.ScrollToCaret();
        }

        private void toolPrintScreen_ButtonClick(object sender, EventArgs e)
        {

        }

        private void toolImgFile_Click(object sender, EventArgs e)
        {

        }

        private void toolVibration_Click(object sender, EventArgs e)
        {

        }

        private void toolCountenance_Click(object sender, EventArgs e)
        {
            skinPanelEmoji.Visible = !skinPanelEmoji.Visible;
        }

        private void toolFont_Click(object sender, EventArgs e)
        {

        }

        private void btnSendMenu_Click(object sender, EventArgs e)
        {

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            var obj = tbSend.GetContent();
            if (!string.IsNullOrWhiteSpace(obj.Text) || obj.EmotionDictionary.Count > 0 || obj.ForeignImageDictionary.Count > 0)
            {
                IMessage msg = new ChatMessage()
                {
                    Data = obj,
                    Receiver = int.Parse(FormItem.Tag.ToString()),
                    Sender = Current.User.ID,
                    Type = MessageType.Chat
                };
                Current.Send(msg);
                AppendMessage("我");
                tbShow.AppendChatBoxContent(obj);
                tbShow.AppendText(Environment.NewLine);
                tbShow.ScrollToCaret();
                tbSend.Clear();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lblChatName_MouseEnter(object sender, EventArgs e)
        {

        }

        private void lblChatName_MouseLeave(object sender, EventArgs e)
        {

        }

        private void ChatForm_Load(object sender, EventArgs e)
        {
            if (FormItem != null)
            {
                lblChatName.Text = FormItem.NicName;
                lblChatQm.Text = FormItem.PersonalMsg;
                pnlImgTx.BackgroundImage = FormItem.HeadImage;
                ChatFont = new Font(tbShow.Font.FontFamily, 9, FontStyle.Regular);
            }
            InitEmoji();
            tbSend.Focus();
            tbSend.Initialize(DefaultEmotions);
            tbShow.Initialize(DefaultEmotions);
        }

        private void InitEmoji()
        {
            int x = 6;
            int y = 3;
            for (int i = 0; i < 70; i++)
            {
                GifBox gb = new GifBox();
                gb.Click += (a, e) =>
                {
                    var ui = uint.Parse(gb.Tag.ToString());
                    tbSend.InsertDefaultEmotion(ui);
                    skinPanelEmoji.Hide();
                };
                gb.Cursor = Cursors.Hand;
                gb.Tag = i.ToString();
                gb.Image = (System.Drawing.Bitmap)Properties.Face.ResourceManager.GetObject("_" + i);
                gb.Width = 21;
                gb.Height = 21;
                gb.Location = new Point(x, y);
                DefaultEmotions.Add((uint)i, gb.Image);
                skinPanelEmoji.Controls.Add(gb);
                if ((i + 1) % 10 == 0)
                {
                    x = 6;
                    y += 23;
                }
                else
                {
                    x += 23;
                }
            }
        }
    }
}
